#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>
#include <QTime>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow),
	addDialog(new addWordDialog(this)),
	dialogTraining(new DialogTraining(this)),
	data(new DataFile),
	englishList(new WordList),
	germanList(new WordList)
{
	ui->setupUi(this);
	QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
	qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
	ui->tableWidget->selectRow(0);
	currentFileName = "English.xml";
	data->readList(englishList, "English.xml");
	data->readList(germanList, "German.xml");
	showList(englishList);
	connect(addDialog, SIGNAL(accepted(WordList*)), this, SLOT(addWords(WordList*)));
	connect(dialogTraining, SIGNAL(accepted()), this, SLOT(updateTable()));
	connect(ui->actionAdd, SIGNAL(triggered()), addDialog, SLOT(exec()));
	connect(ui->tableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(showWord()));
}
void MainWindow::addWords(WordList *list) {
	data->merge(p_currentList, list);
	updateTable();
}
void MainWindow::showWord() {
	ui->labelSelectedWords->setText(tr("Выделено слов: ") + num(ui->tableWidget->selectedItems().size()));
	if(ui->tableWidget->selectedItems().size() == 0)
		return;
	WordItem *wordItem = p_currentList->at(ui->tableWidget->selectedItems().at(0)->data(1).toInt());
	ui->textLabel->setText(wordItem->word());
	ui->translateLabel->setText(wordItem->translate());
	ui->transcriptionLabel->setText(wordItem->transcription());
	ui->scoreLabel->setText("<font color=\"green\">" + num(wordItem->plusesCount()) + "</font>/<font color=\"gray\">" + num(wordItem->minusesCount()) + "</font>");
}
void MainWindow::on_actionEnglish_triggered() {
	showList(englishList);
	currentFileName = "English.xml";
}
void MainWindow::on_actionGerman_triggered() {
	showList(germanList);
	currentFileName = "German.xml";
}
void MainWindow::on_buttonStart_clicked() {
	WordList *list = new WordList;
	if(ui->radioRandom->isChecked()) {
		for(int i = 0; i < ui->lineEditNumberWords->text().toInt(); i++)
			list->append(p_currentList->at(qrand() % p_currentList->size()));
	} else {
		QList<QTableWidgetItem*> selected = ui->tableWidget->selectedItems();
		for(int i = 0; i < selected.length(); i++)
			list->append(p_currentList->at(selected.at(i)->data(1).toUInt()));
	}
	dialogTraining->setWordList(list);
	dialogTraining->setType(ui->comboBoxTranslateType->currentIndex());
	dialogTraining->beginTraining();
	dialogTraining->exec();
	list->clear();
	data->writeList(p_currentList, currentFileName);
}
void MainWindow::showList(WordList *list) {
	p_currentList = list;
	updateTable();
}
void MainWindow::updateTable(void) {
	ui->tableWidget->clear();
	ui->tableWidget->horizontalHeader()->setDefaultSectionSize(254);
	ui->tableWidget->horizontalHeader()->setVisible(false);
	ui->tableWidget->verticalHeader()->setVisible(false);
	ui->lineEditNumberWords->setText(num(p_currentList->size()));
	data->writeToTable(p_currentList, ui->tableWidget);
}
MainWindow::~MainWindow() {
	delete ui;
	dialogTraining->deleteLater();
	addDialog->deleteLater();
	delete data;
	englishList->clear();
	germanList->clear();
	p_currentList->clear();
}
